//
//  HOSearchCompanyViewController.m
//  微度报修
//
//  Created by foguo-mac-1 on 2/28/17.
//  Copyright © 2017 WeiDu. All rights reserved.
//

#import "HOSearchCompanyViewController.h"
#import "CompanyEntity.h"
#import "HOConstants.h"


@interface HOSearchCompanyViewController ()
{
}
@end

@implementation HOSearchCompanyViewController

- (void)viewDidLoad {
	[super viewDidLoad];
	[self initSearchView];
//	[self initTableViewWithPullUp];
    // Do any additional setup after loading the view.
	//.[self initData];
}

- (void)initData
{
	/*for(int i = 0; i < 10; i ++)
	{
		CompanyEntity *entity = [[CompanyEntity alloc] init];
		entity.companyId = [NSString stringWithFormat:@"0000%d", i];
		entity.companyName = [NSString stringWithFormat:@"医院名字:%d", i];
		[searchView.dataList addObject:entity];
		[entity release];
	}*/
}

- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	[self setNavigationItemTitle:NSLocalizedString(@"查找医院号", @"查找医院号")];
	[self gotoSearchKey:@""];
}


- (void)gotoSearchKey:(NSString *)key
{
	//if(key == nil || key.length == 0)
	//	return;
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_SearchCompany, kHO_FIELD_CompanyName:key}];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
	return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return 44;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [searchView.dataList count];
}


- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
	return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *AMBaseTableViewCellIdentifier = @"HOTableViewCell";
	HOTableViewCell *cell = (HOTableViewCell *)[tableView dequeueReusableCellWithIdentifier:AMBaseTableViewCellIdentifier];
	if(cell == nil)
	{
		cell = [[[HOTableViewCell alloc]
				 initWithStyle:UITableViewCellStyleDefault
				 reuseIdentifier: AMBaseTableViewCellIdentifier] autorelease];
		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	}
	CompanyEntity *companyEntity = [searchView.dataList objectAtIndex:indexPath.row];
	UILabel *nameLbl = (UILabel *)[cell viewWithTag:110];
	if(nameLbl == nil)
	{
		nameLbl = [[UILabel alloc] initWithFrame:CGRectMake(10, (44 - 22) / 2, tableView.frame.size.width - 130, 22)];
		nameLbl.tag = 110;
		nameLbl.font = [UIFont systemFontOfSize:17];
		[cell.contentView addSubview:nameLbl];
//		nameLbl.layer.borderWidth = 1;
		[nameLbl release];
	}
	nameLbl.text  = companyEntity.companyName;
	
	UILabel *lbl = (UILabel *)[cell viewWithTag:111];
	if(lbl == nil)
	{
		lbl = [[UILabel alloc] initWithFrame:CGRectMake(tableView.frame.size.width - 110, (44 - 22) / 2, 80, 22)];
		lbl.tag = 111;
		lbl.textAlignment = NSTextAlignmentRight;
		lbl.font = [UIFont systemFontOfSize:14];
		lbl.textColor = [UIColor grayColor];
		[cell.contentView addSubview:lbl];
//		lbl.layer.borderWidth = 1;
		[lbl release];
	}
	lbl.text  = companyEntity.companyId;
	
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	CompanyEntity *companyEntity = [searchView.dataList objectAtIndex:indexPath.row];
	[self sendCommand:kHO_CMD_SELECDTED_COMPANY data:companyEntity];
	[self goBack];
}
#pragma mark

- (void)requestDidSuccessWithResponse:(HOBaseResponseEntity *)response
{
	if(![response.action isEqualToString:kHO_ACTION_SearchCompany])
		return;
	[searchView.dataList removeAllObjects];
	for(int i = 0; i < [response.dataNode count]; i ++)
	{
		NSDictionary *dic = [response.dataNode objectAtIndex:i];
		CompanyEntity *entity = [[CompanyEntity alloc] init];
		[entity parseDic:dic];
		[searchView.dataList addObject:entity];
		[entity release];
	}
	
	[searchView reloadData];
	
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
